/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Shuriken.Core.UI
{
    /// <summary>
    /// This class is for helping the UI to find out how toi execute the give list of slots
    /// </summary>
    class ExecutionHelper
    {
        /// <summary>
        /// Executes the given list of slots
        /// </summary>
        /// <param name="slots"></param>
        public static void Execute(List<IUIDisplaySlot> slots)
        {
            Shuriken.Base.ISubject subject;
            Shuriken.Base.IAction action; 

            System.Diagnostics.Trace.Assert(slots != null, "ExecutionHelper.Execute received a null object!");
            System.Diagnostics.Trace.Assert(slots.Count >= 2, "ExecutionHelper.Execute received an empty list!");
            System.Diagnostics.Trace.Assert(slots[0].SelectedDisplayItem != null, "ExecutionHelper.Execute received a list with null subject element!");
            System.Diagnostics.Trace.Assert(slots[1].SelectedDisplayItem != null, "ExecutionHelper.Execute received a list with null actionelement!");

            subject = (Shuriken.Base.ISubject)slots[0].SelectedDisplayItem;
            action = (Shuriken.Base.IAction)slots[1].SelectedDisplayItem;

            Shuriken.Core.Utils.Tracer.WriteInfo(subject.ToString() + " selected for exection.");
            Shuriken.Core.Utils.Tracer.WriteInfo(action.ToString() + " selected for exection.");

            if (action.IsAttributed)
            {
                action.ActionAttribute = slots[2].DisplayText;
            }

            Shuriken.Core.Ranking.RankingService.DefaultInstance.RegisterHit((Shuriken.Core.Store.ISearchable)subject);

            action.Execute(subject);

            Shuriken.Core.Utils.Tracer.WriteWarning(subject.Name + " is executed with " + action.Name + " action.");     
        }
    }
}
